import {Component, OnInit, Input, Injector, Output, EventEmitter } from '@angular/core';
import {CoreComponent, Dynamic, TabView} from 'pengesoft-ng-lib';
import { RecruitmentPlan } from '../../../domains/recruitmentplan.domain';
import {RecruitmentPlanMgeSvrService} from '../../../services/recruitment-plan-mge-svr.service';
import {NzCascaderOption} from 'ng-zorro-antd/cascader';
import {JobServiceSvr} from '../../../services/job.service';
import { Resume} from '../../../domains/resume.domain';

@Component({
  selector: 'app-re-application-add',
  templateUrl: './re-application-add.component.html',
  styleUrls: ['./re-application-add.component.less']
})

@TabView('分组')
@Dynamic()
export class ReApplicationAddComponent extends CoreComponent implements OnInit {
  Reason: string ;
  Way: string;
  date = null;

  @Input() info: any;
  @Input() optype: any;                    // 接收操作类型 1.添加，2.修改
  @Input() depart: NzCascaderOption[];     // 接受所有部门
  @Input() infoLi: any;                    // 接受详细信息
  @Input() Jobvalue: any;                  // 接受岗位默认值
  @Input() departDefault: any;             // 接收部门默认值
  @Input() JobList: any = [];              // 接受该部门下岗位列表
  // @ts-ignore
  nzOptions: NzCascaderOption[] = this.options;
  // @ts-ignore

  // infoList = new RecruitmentPlan();
  constructor(
    private injector: Injector,
    public recruSvr: RecruitmentPlanMgeSvrService,
    public jobSvr: JobServiceSvr,
  ) {
  super(injector);
  }

  ngOnInit(): void {
    this.Reason = this.infoLi.Reason + '';        // 后端返回数据转换
    this.Way = this.infoLi.Way + '';
  }

  // 添加按钮点击
  addSubmit(): void{
    // tslint:disable-next-line:radix
    this.infoLi.Reason = parseInt(this.Reason);
    // tslint:disable-next-line:radix
    this.infoLi.Way = parseInt(this.Way);
    this.recruSvr.addRecruitment(this.infoLi).then( res => {
      if (res === 200){
        this.nzMessageSvr.success('添加成功');
        this.infoLi = new RecruitmentPlan();
      }else {
        this.nzMessageSvr.error('添加失败,请确认输入是否有误');
      }
    }, err => {
          this.nzMessageSvr.error(err.Msg);
    });
  }

  // 修改按钮点击
  updateSubmit(): void{
    // tslint:disable-next-line:max-line-length radix
    this.infoLi.Reason = parseInt(this.Reason);
    // tslint:disable-next-line:radix
    this.infoLi.Way = parseInt(this.Way);
    this.recruSvr.updateRecruitment(new RecruitmentPlan(this.infoLi)).then( res => {
      if (res === 200){
        this.nzMessageSvr.success('修改成功');
      }else {
        this.nzMessageSvr.error('修改失败,请确认输入是否有误');
      }
    }, err => {
      this.nzMessageSvr.error(err.msg);
    });
  }

  // 监听部门改变事件 发请求获取相应岗位
  onChangesDep(values: string[]): void {
    this.JobList = [];
    this.JobList[0] = new Resume();
    this.Jobvalue = {};
    // tslint:disable-next-line:radix
    this.infoLi.DeptId = parseInt(values[values.length - 1]);
    this.jobSvr.getJobByDept(values[values.length - 1] + '').then(res => {
      // tslint:disable-next-line:forin
      for (const i in res){
        res[i].label = res[i].JobName;
        res[i].value = res[i].Id;
      }
      this.JobList = res;
      // this.Jobvalue = this.JobList[0].Id;
      this.JobList.JobId = this.Jobvalue;
    }, err => {
      this.nzMessageSvr.error(err.Msg);
    });
  }

  // 监听岗位改变，获取岗位值
  // tslint:disable-next-line:ban-types
  onChangesJob(values: String[]): void{
      this.infoLi.JobId = values + '';
  }
}


